package com.sardine.base.http;

import com.sardine.base.utils.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

/**
 * 北京京东世纪贸易有限公司<br>
 * Created with IntelliJ IDEA.
 * Date:2016/12/8
 * Time:16:39
 *
 * @author daifeng<br>
 *         daifeng@jd.com
 * @since JDK 1.7 <br>
 * Copyright (c) 2016 JD System Incorporated All Rights Reserved.
 */
public class RSATester {
    private static final Logger logger = LoggerFactory.getLogger(RSATester.class);
    static String publicKey;
    static String privateKey;

    static {
        try {
            Map<String, Object> keyMap = RSAUtils.genKeyPair();
            publicKey = RSAUtils.getPublicKey(keyMap);
            privateKey = RSAUtils.getPrivateKey(keyMap);
            System.err.println("公钥: \n\r" + publicKey);
            System.err.println("私钥： \n\r" + privateKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
//        test();
//        testSign();
        testSign02();
    }

    static void test() throws Exception {
        System.out.println("公钥加密——私钥解密");
        String source = "这是一行要进行RSA加密的原始数据&100";
        System.out.println("加密前文字：" + source);
        byte[] data = source.getBytes();
        //公钥加密
        byte[] encodedData = RSAUtils.encryptByPublicKey(data, publicKey);
        System.out.println("加密后文字： " + Base64.encode(encodedData));
        //私钥解密
        byte[] decodedData = RSAUtils.decryptByPrivateKey(encodedData, privateKey);
        String target =new String(decodedData);
        System.out.println("解密后文字:  " + target);
    }

    static void testSign() throws Exception {
        System.out.println("--------------------------------------------------------------------");
        System.out.println("私钥加密——公钥解密");
        String source = "caseId=";
        System.out.println("原文字：" + source);
        byte[] data = source.getBytes();
        //私钥加密
        byte[] encodedData = RSAUtils.encryptByPrivateKey(data, privateKey);
        System.out.println("加密后： " + Base64.encode(encodedData));
        //公钥解密
        byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKey);
        String target = new String(decodedData);
        System.out.println("解密后:  " + target);

        System.out.println("私钥签名——公钥验证签名");
        String sign = RSAUtils.sign(encodedData, privateKey);
        System.out.println("签名: " + sign);
        boolean status = RSAUtils.verify(encodedData, publicKey, sign);
        System.out.println("验证结果: " + status);
    }

    static void testSign02() throws Exception {
        String privateKey01="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIz9IurhbEWyZtDVRvpmbH9sCOYO+E1NJi88/x8vTgY8dyf6zLZTr4oxIGz05N3oPtlkTQZAHk0WqupazabfGLUZnYIxH85jLD+00/vFmqDndptPrbClxk1CX5zAWZ7issE1gqJeqWGKQ8YJcaZ7+bfH3j15pjeOrTmrc08ojCUtAgMBAAECgYBKLbEeZUYjEW8Y9u2mNxdkn0G7USsA+NNbTTtpe+kQTOXgagsk7rjP+3L1AJAd1OCkXipMXJXUt1VrgiTrZDvJ1YpSbwYW23Hjl8Pi+AIt9vF0IVqC+x42PJwFmG/dAfpRYpV51Tk4jFUkmQpv2rd6uYc3fmfOql5dFG5i4vXWfQJBAPkWv16JelICc89w9gbz/ZJtO9PUQNR3zRBDQuDAcO0qdzchAtJJ6Xjk3NO3irBnprSBSh5lBAMnuamr8OHTYTcCQQCQ5pC+G13gy5pkkf20dMatwTy3g6r6UUZcdR2MBMrne6hQT3XJ1sXmo2inx7BES8EqwBSsJgNDUbYvJUyU1e67AkBxUqAN6o3z8Zmurr2/vBGJJJkrbk1wXHC3DWLHzcWSoNdMBYC7G1VU4wp0Z/R4/S8iwA9wdJS2usQFJWyZ0QlRAkAjdakaPMw+hNTlo6x4itNMFivrpPckhts3KRnv+ARHncVU0f07RanshTTJSl7id79v3M2ySyMlSEvTQcd57NidAkEAjv8n3DpcgVEklXnGAXWzIcS0BHUjvfWQv7gip/cStCIadGlgouDOvzuZ+Y4SY1tnDwPIdvu2SvntoFtrlZtfGQ==";
        String publicKey01="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCM/SLq4WxFsmbQ1Ub6Zmx/bAjmDvhNTSYvPP8fL04GPHcn+sy2U6+KMSBs9OTd6D7ZZE0GQB5NFqrqWs2m3xi1GZ2CMR/OYyw/tNP7xZqg53abT62wpcZNQl+cwFme4rLBNYKiXqlhikPGCXGme/m3x949eaY3jq05q3NPKIwlLQIDAQAB";
        System.out.println("--------------------------------------------------------------------");
        StringBuffer sb=new StringBuffer();
//        sb.append("content");
//        sb.append("=");
//        sb.append("内容很猥琐");
//        sb.append("&");
//        sb.append("imgUrls");
//        sb.append("=http://**0.jpg,http://**1.jpg");
//        sb.append("&");
//        sb.append("operateDate");
//        sb.append("=2017-01-05 12:12:12");
//        sb.append("&");
        sb.append("orderId");
        sb.append("=");
        sb.append("263046992240827");
        sb.append("&");
        sb.append("phone");
        sb.append("=13559797959");
        sb.append("&");
        sb.append("pin");
        sb.append("=13559797959");
        sb.append("&");
        sb.append("type");
        sb.append("=10");
        String source = sb.toString();
        System.out.println("原文字：" + source);
        byte[] data = source.getBytes();
        System.out.println("私钥签名——公钥验证签名");
        String sign = RSAUtils.sign(data, privateKey01);
        System.out.println("签名: " + sign);
        boolean status = RSAUtils.verify(data, publicKey01, sign);
        System.out.println("验证结果: " + status);

        ArrayList<String> dd=new ArrayList<>();
        dd.add("content");
        dd.add("orderId");
        dd.add("phone");
        dd.add("pin");
        dd.add("imgUrls");
        dd.add("type");
        dd.add("operateDate");
        Collections.sort(dd);
        System.out.println(dd.toString());
    }
}
